<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>
            阳光成单系统
        </title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="format-detection" content="telephone=no">
        <link rel="stylesheet" href="./css/x-admin.css" media="all">
    </head>
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>会员管理</cite></a>
              <a><cite>权限规则</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
        </div>
        <div class="x-body">
            <form class="layui-form x-center"  id ="form_news" style="width:70%" onsubmit="return false">
                <div class="layui-form-pane" style="margin-top: 15px;">
                  <div class="layui-form-item">
                    <div class="layui-input-inline">
                        <select id="newsCategoryId" name="newsCategoryId">
                            <option value="">请选择新闻类别</option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                      <input type="text" name="newsName"  placeholder="新闻标题" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-input-inline" style="width:80px">
                        <button class="layui-btn" onclick = "myPage()"  lay-filter="*"><i class="layui-icon">&#xe615;</i>搜索</button>
                    </div>
                  </div>
                </div> 
            </form>
            <xblock><button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon">&#xe640;</i>批量删除</button>
            <button class="layui-btn"
			onclick="news_add('添加新闻详情','news-add.html','800','500')">
			<i class="layui-icon" >&#xe608;</i>添加
		</button>
		<span class="x-right" style="line-height:40px">共有数据：<span id ="count"></span>条</span></xblock>
            <table class="layui-table">
                <thead>
                    <tr>
                        <th><input onclick="selectAll(this)" type="checkbox" name="" value=""></th>
                        <th>
                            新闻ID
                        </th>
                        <th>
                            新闻标题
                        </th>
                        <th>
                            新闻时间
                        </th>
                        <th>
                            新闻内容
                        </th>
                        <th>
                            所属分类
                        </th>
                        <th>
                            操作
                        </th>
                    </tr>
                </thead>
                <tbody id="tbody_news">
                    
                </tbody>
            </table>

            <div id="page"></div>
        </div>
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script src="./js/x-layui.js" charset="utf-8"></script>
        <script>
    	var $ = null;
    	var laypage = null;
    	var layer = null;
    	var form = null;
                layui.use(['element','laypage','layer','form'], function(){
                   $ = layui.jquery;//jquery
                  laypage = layui.laypage;//分页
                  layer = layui.layer;
                  form = layui.form;
                  $(function(){
                	  myPage();
                	  newscate();
    				});
                });
              //分页显示
       		 function myPage(){
               	 //执行一个laypage实例
               	 $.post("../adminNews?reqName=getAllNews",$("#form_news").serialize(),function(res){
               		 $("#count").html(res);
               	  laypage.render({
               	    elem: 'page', //注意，这里的 test1 是 ID，不用加 # 号
               	    count: res,//数据总数，从服务端得到
               	    limit: 3,
               	    jump: function(obj, first){
               	     	selNews(obj.curr,obj.limit);
               	      }
               	  });
               	 },"json");
               }
              //动态获取下拉框
       		function newscate(){
            	$.post("../adminNewsCategory?reqName=getAllNewsCates",{},function(res){
            		for(var i=0;i<res.length;i++){
            		$("#newsCategoryId").append("<option value="+res[i].newsClassIficationId+">"+res[i].newsClassIficationName+"</option>");
            			form.render("select");
            		}
            	},"json");
            }
            //查询所有新闻
           function selNews(page,size){
           	$("#tbody_news").html("");//发送请求前tbody表格内容
           	$.post("../adminNews?reqName=getNews",$.param({page:page,size:size})+'&'+$("#form_news").serialize(),function(res){
           		for(var i = 0;i<res.length;i++){
           			$("#tbody_news").append(` <tr>
                               <td>
                               <input type="checkbox" value="1" name="">
                           </td>
                           <td>${res[i].newsId}</td>
                           <td>
                           ${res[i].newsName}
                           </td>
                           <td>
                           ${res[i].newsTime}
                           </td>
                           <td>
                           ${res[i].newsContent}
                           </td>
                           <td>
                           ${res[i].newsClassIficationName}
                           </td>
                           
                           <td class="td-manage">
                               <a title="编辑" href="javascript:;" onclick="news_edit('编辑新闻','news-edit.html','${res[i].newsId}','','510')"
                               class="ml-5" style="text-decoration:none">
                                   <i class="layui-icon">&#xe642;</i>
                               </a>
                               <a title="删除" href="javascript:;" onclick="News_del(this,${res[i].newsId})" 
                               style="text-decoration:none">
                                   <i class="layui-icon">&#xe640;</i>
                               </a>
                           </td>
                       </tr>`);
           		}
           	},"json");
           }
            //全选或全不选
           function selectAll(obj){
         	   var val = $(obj).prop("checked");
         	  $("#tbody_news input:checkbox").prop("checked",val)
           }

         //批量删除提交
          function delAll () {
         var chks=$("#tbody_news input:checkbox:checked");
         	if(chks.length == 0){
         		layer.alert("您未选择任何行!");
         		return false;
         	}
             layer.confirm('确认要删除吗？',function(index){
                var ids= [];
             	for(var i=0;i<chks.length;i++){
                 	var id=$(chks[i]).parent().next().html();
                 	ids.push(id);
                 }
             	$.post("../adminNews?reqName=deleteNewsByIds",{
             		newsId:ids.join(",")
             	},function(res){
             		if(res.success){
             			layer.msg('删除成功', {icon: 1});
             			myPage();
             		}else{
             			 layer.msg('删除失败', {icon: 3});
             		}
             	},"json")
             });
          }
            
			//编辑新闻
            function news_edit (title,url,id,w,h) {
            	sessionStorage.setItem("newsId",id);
                x_admin_show(title,url,w,h); 
            }
            /*添加*/
            function news_add(title,url,w,h){
                x_admin_show(title,url,w,h); 
                
            }
            /*-删除*/
    		function News_del(obj, id) {
    			layer.confirm('确认要删除吗？', function(index) {
    				//发异步删除数据
    				$.post("../adminNews?reqName=deleteNews",{
    					newsId : id
    				},function(res){
    					if (res.success) {//删除成功
    						layer.msg(res.message, {icon : 1,time : 2000});
    						$(obj).parents("tr").remove();//删除的所在行
    					} else {//删除失败
    						layer.msg(res.message, {icon : 1,time : 2000});
    					}
    				},"json")
    			});
    		}
            </script>
            
    </body>
</html>